$(document).ready(function() {
	var queryKey = getParameter("jquery.groovelist.js", "querykey");

	$.ajax({
		type : "POST",
		// contentType: "application/json",
		// //这段代码不要加，我时延的是否后台会接受不到数据
		url : context
				+ "/system/jquery.datatable.arg.do?method=modelcmp&querykey="
				+ queryKey,
		dataType : "json",
		data : {
			queryKey : queryKey
		}, // 以json格式传递(struts2后台还是以string类型接受),year和month直接作为参数传递。
		success : function(modelcmpData) {
			if (!modelcmpData.success) {
				alert(modelcmpData.errorMsg);
				return false;
			}

			$("#exampleTitle").html(modelcmpData.resultData.name);

			var aoColumnDefs = [];

			$(modelcmpData.resultData.columnModel).each(function(i, n) {
				aoColumnDefs.push({
					"mRender" : function(data, type, row) {
						if (n.mData == "datatable_chk") {
							return "<input type=\"checkbox\" name=\"datatable_chk\" value=\""
									+ data + "\">";
						} else {
							try {
								return eval(n.fnRenderName
										+ "(data, type, row, '" + n.mData
										+ "');");
							} catch (err) {
								alert(err.name + " : " + err.message);
							}
						}
					},
					"aTargets" : [i],
					"bVisible" : (n.show != "-1"),
					"sClass" : "center",
					"bSortable" : (n.mData != "datatable_chk"
							&& n.mData != "PAGINATION_NUMBER" && n.alien != "1")
				});
			});

			var oTable = $('#example').dataTable({
				"bProcessing" : true,
				"bJQueryUI" : true,
				"bServerSide" : true,
				"sPaginationType" : "full_numbers",
				/*
				 * "sAjaxSource" : context +
				 * "/system/jquery.datatable.arg.do?method=list&querykey=fcapplylist",
				 */
				// "sAjaxSource" : context + "/app/jqueryList/js/objects.js",
				"iDisplayLength" : 20,
				"fnServerData" : function(sSource, aoData, fnCallback) {
					$.ajax({
						type : "POST",
						// contentType: "application/json",
						// //这段代码不要加，我时延的是否后台会接受不到数据
						url : context
								+ "/system/jquery.datatable.arg.do?method=list&querykey="
								+ queryKey,
						dataType : "json",
						data : {
							"jsonparam" : JSON.stringify(arrayToObject(aoData))
						}, // 以json格式传递(struts2后台还是以string类型接受),year和month直接作为参数传递。
						success : function(data) {
							fnCallback(data); // 服务器端返回的对象的returnObject部分是要求的格式
						}
					});
				},
				"aoColumns" : modelcmpData.resultData.columnModel,
				"aoColumnDefs" : aoColumnDefs,
				// "aoColumnDefs" : modelcmpData.resultData.aoColumnModel,
				"oLanguage" : {
					"sSearch" : "查询: _INPUT_",
					"sLoadingRecords" : "",
					"sProcessing" : "",
					"oAria" : "",
					"sLengthMenu" : "每页显示 _MENU_ 条数据",
					"sZeroRecords" : "无数据信息",
					"sInfo" : "显示 _START_ 至 _END_ &nbsp;共 _TOTAL_ 条",
					"sInfoEmpty" : "无数据信息",
					"sInfoFiltered" : "(在 _MAX_ 条数据信息中进行过滤)",
					"oPaginate" : {
						"sFirst" : "首页",
						"sPrevious" : "上一页",
						"sNext" : "下一页",
						"sLast" : "尾页"
					}
				}
			});

			if (modelcmpData.resultData.showchk) {
				$('#example thead tr th:first').attr("title", "点击全部选择");

				$('#example thead tr th:first').live('click', function() {
					if ($(this).attr("chkall") == "yes") {
						$('#example thead tr th:first').attr("title", "点击全部选择");
						$(this).attr("chkall", "no");
						$("input[name='datatable_chk']").attr("checked", false);
					} else if ($(this).attr("chkall") == null
							|| $(this).attr("chkall") == "no") {
						$('#example thead tr th:first').attr("title", "点击取消选择");
						$(this).attr("chkall", "yes");
						$("input[name='datatable_chk']").attr("checked", true);
					}
				});
			}

			if (modelcmpData.resultData.toolbars != null
					&& modelcmpData.resultData.toolbars.length > 0) {

			}

			defaultFnToolbar($("#exampleToolbar"));
		}
	});

});

function arrayToObject(aoData) {
	var obj = {};

	if (aoData != null) {
		$(aoData).each(function(i, n) {
					obj[n.name] = n.value;
				})
	}

	return obj;
}

/**
 * default fnRender
 * 
 * @param {}
 *            data
 * @param {}
 *            type
 * @param {}
 *            row
 * @return {}
 */
function defaultFnRender(data, type, row, fieldName) {
	return data;
}

function defaultFnToolbar(tlb) {
}